package org.anti996.lms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.anti996.lms.model.Booking;
import org.anti996.lms.mapper.BookingMapper;
import org.anti996.lms.model.BookingOutput;
import org.anti996.lms.service.BookingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 货单 服务实现类
 * </p>
 *
 * @author The Anti996 Authors
 * @since 2022-07-15
 */
@Service
public class BookingServiceImpl extends ServiceImpl<BookingMapper, Booking> implements BookingService {
    @Override//根据货单号查询
    public Booking findBybNum(Integer bNum) {
        //条件构造器
        QueryWrapper<Booking> wrapper = new QueryWrapper<>();
        wrapper.eq("b_Num",bNum);
        return baseMapper.selectOne(wrapper);
    }

    @Override//根据发货客户查询
    public Booking findBycIdFrom(Integer cIdFrom) {
        QueryWrapper<Booking> wrapper = new QueryWrapper<>();
        wrapper.eq("c_id_from",cIdFrom);
        return (Booking) baseMapper.selectList(wrapper);
    }

    @Override//根据收货客户查询
    public Booking findBycIdTo(Integer cIdTo) {
        QueryWrapper<Booking> wrapper = new QueryWrapper<>();
        wrapper.eq("c_id_to",cIdTo);
        return (Booking) baseMapper.selectList(wrapper);
    }

    @Override//根据发货仓库查询
    public Booking findBywIdFrom(Integer wIdFrom) {
        QueryWrapper<Booking> wrapper = new QueryWrapper<>();
        wrapper.eq("w_id_from",wIdFrom);
        return (Booking) baseMapper.selectList(wrapper);
    }

    @Override//根据收货仓库查询
    public Booking findBywIdTo(Integer wIdTo) {
        QueryWrapper<Booking> wrapper = new QueryWrapper<>();
        wrapper.eq("w_id_to",wIdTo);
        return (Booking) baseMapper.selectList(wrapper);
    }

    @Override//根据运单号查询
    public List<Booking> findBytNum(Integer tNum) {
        QueryWrapper<Booking> wrapper = new QueryWrapper<>();
        wrapper.eq("t_Num",tNum);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<BookingOutput> listAllOutput() {
        var rs = baseMapper.findAllBookings();
        for (var op : rs) {
            op.setStatus(BookingOutput.calculateStatus(op));
        }
        return rs;
    }

    @Override
    public List<BookingOutput> listAllOutputByOperator(String username) {
        var rs = baseMapper.findAllBookingsByOperator(username);
        for (var op : rs) {
            op.setStatus(BookingOutput.calculateStatus(op));
        }
        return rs;
    }

    @Override
    public List<BookingOutput> listAllOutputByWarehouse(String username) {
        var rs = baseMapper.findAllBookingsByWarehouse(username);
        for (var op : rs) {
            op.setStatus(BookingOutput.calculateStatus(op));
        }
        return rs;
    }

    @Override
    public BookingOutput findById(int b_num) {
        var rs = baseMapper.findBookingById(b_num);
        rs.setStatus(BookingOutput.calculateStatus(rs));
        return rs;
    }

    @Override//分页查询,输入第几页，每页几条消息
    public Map<String, Object> findPage(int current, int limit) {
        //创建分页对象
        Page<Booking> page = new Page<>(current,limit);
        //调用分页查询的方法
        baseMapper.selectPage(page,null);
        //获取表中显示的数据
        List<Booking> list = page.getRecords();
        //获取表中总记录数
        long total = page.getTotal();
        //获取分页总数
        long pages = page.getPages();
        //将分页信息数据封装在Map集合中
        Map<String,Object> map = new HashMap<>();
        map.put("rows",list);
        map.put("total",total);
        map.put("pages",pages);
        map.put("current",current);
        map.put("limit",limit);
        return map;
    }

    @Override //查询已到达未签收货单
    public List<Booking> finduncheckout(Integer w_id){
        QueryWrapper<Booking> wrapper = new QueryWrapper<>();
        wrapper.eq("w_id_to",w_id);
        wrapper.isNotNull("arrive");
        wrapper.isNull("check_out");
        return baseMapper.selectList(wrapper);
    }

    @Override//查询所有已结算账单
    public List<Booking> showAllmoney(){
        QueryWrapper<Booking> wrapper = new QueryWrapper<>();
        wrapper.isNotNull("check_out");
        return baseMapper.selectList(wrapper);
    }
}

